package com.fg.cloud.system.controller.factory;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.factory.FactoryEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryVo;
import com.fg.cloud.manager.service.factory.FactoryService;
import com.fg.cloud.system.controller.BaseSystemController;

/**
 * Description:新建厂商
 * 
 * @author shaoqiping
 * @date 2017年8月23日下午1:51:28
 */
@Controller
@RequestMapping("/factoryCreate")
public class FactoryCreateController extends BaseSystemController {
	
	@Autowired
	private FactoryService factoryService;
	
	@RequestMapping(value = "/facInfo")
	public String facInfo(HttpServletRequest req, HttpServletResponse resp){
		return "factory/factoryList";
	}
	
	/**
	 * Description:分页查询所有厂商信息
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param order
	 * @param sort
	 * @author dingjiang
	 * @date 2017年10月25日上午10:56:03
	 */
	@RequestMapping(value = "/selectPage", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public PageUtils<FactoryEntity> selectPage(HttpServletRequest req, HttpServletResponse resp, 
			Integer page, Integer rows, String order, String sort, String keyword, String beginTime,
			String endTime) {
		try {
			if ( null == page ) page = 1;
			if ( null == rows ) rows = 10;
			PageUtils<FactoryEntity> pageinfo = new PageUtils<>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("keyword", keyword);
			condition.put("beginTime", beginTime);
			condition.put("endTime", endTime);

			factoryService.selectPage(pageinfo);
			pageinfo.setRows(pageinfo.getData());
			return pageinfo;
		} catch (Exception e) {
			e.printStackTrace();
			return new PageUtils<>();
		}
	}
	
	/**
	 * Description:根据ID查询厂商信息
	 * @param req
	 * @param resp
	 * @param id
	 * @return
	 * @author dingjiang
	 * @date 2017年10月27日上午11:47:04
	 */
	@RequestMapping(value = "/selectOne", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public FactoryVo selectById(HttpServletRequest req, HttpServletResponse resp, Long id){
		try {
			FactoryVo factoryVo = factoryService.find(id);
			return factoryVo;
		} catch (Exception e) {
			e.printStackTrace();
			return new FactoryVo();
		}
	}
	
	/**
	 * Description:批量删除
	 * @param req
	 * @param resp
	 * @param ids
	 * @author dingjiang
	 * @date 2017年10月26日上午11:36:33
	 */
	@RequestMapping(value = "/delBy", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public String delBy(HttpServletRequest req, HttpServletResponse resp, String ids){
		try {
			boolean result = factoryService.deletes(LongUtils.convert(ids.split("[, ]+")));
			return result ? "ok" : "no";
		} catch (Exception e) {
			e.printStackTrace();
			return "no";
		}
	}

	/**
	  * Description:新建厂商
	  * @param request
	  * @param response
	  * @param factoryEntity
	  * @author shaoqiping
	  * @date 2017年8月23日下午3:19:25
	*/
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public void insert(HttpServletRequest req, HttpServletResponse resp, FactoryVo factoryEntity) {
		try {
			//条件非空验证
			if (factoryEntity == null || StringUtils.isEmpty(factoryEntity.getFacName())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "厂商名称不能为空");
				return ;
			}
			FactoryEntity temp = factoryService.findByFacName(factoryEntity.getFacName());
			//厂商名称查重
			if (temp != null){
				doResult(resp, false, StatusCode.SAVEERROR, null, "厂商名称已存在");
				return ;
			}
			//处理省市区
			String administrative = factoryEntity.getAdministrative();
			if( administrative != null && StringUtils.isNotEmpty(administrative) ) {
				String[] array = administrative.split("[,]+");
				factoryEntity.setProvince(array[0]);
				factoryEntity.setCity(array[1]);
				factoryEntity.setArea(array[2]);
			}
			
			setCreateInfo(factoryEntity, req);
			setUpdateInfo(factoryEntity, req);
			
			boolean flag = factoryService.insertSelective(factoryEntity);
			doResult(resp, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR , null, 
					 flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT );
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
		}
	}
	
	/**
	 * Description:修改厂商信息
	 * @param req
	 * @param resp
	 * @param factoryVo
	 * @author dingjiang
	 * @date 2017年10月30日下午5:52:14
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public void update(HttpServletRequest req, HttpServletResponse resp, FactoryVo factoryVo){
		try {
			//条件非空验证
			if (factoryVo == null || StringUtils.isEmpty(factoryVo.getFacName())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "厂商名称不能为空");
				return ;
			}
			//厂商名称查重
			boolean flag = factoryService.findByNameId(factoryVo.getFacName(), factoryVo.getId());
			if ( flag ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "厂商名称已存在");
				return ;
			}
			
			//处理省市区
			String administrative = factoryVo.getAdministrative();
			if( administrative != null && StringUtils.isNotEmpty(administrative) ) {
				String[] array = administrative.split("[,]+");
				factoryVo.setProvince(array[0]);
				factoryVo.setCity(array[1]);
				factoryVo.setArea(array[2]);
			}
			
			setUpdateInfo(factoryVo, req);
			boolean flag2 = factoryService.update(factoryVo) > 0 ? true : false;
			doResult(resp, flag2, flag2?StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, null, 
					flag2?ResultType.SAVE_SUCCESS:ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
		}
		
		
	}
	
	
	
	
}
